/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.dusa.worklog.viewer;

import cz.dusa.worklog.controller.WorklogDataListener;
import cz.dusa.worklog.controller.WorklogDataManager;
import cz.dusa.worklog.datalib.Customer;
import cz.dusa.worklog.datalib.Project;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.NbBundle;

/**
 *
 * @author vdusa
 */
class CustomerRootNode extends AbstractNode implements WorklogDataListener<Customer> {
    WorklogDataManager worklogDataManager;
    CustomerChildFactory childFactory;

    public CustomerRootNode(List<Customer> customers, String name, WorklogDataManager worklogDataManager) {
        super(Children.LEAF);
        this.worklogDataManager = worklogDataManager;

        worklogDataManager.addWorklogDataListener((WorklogDataListener) this, Customer.class);
        childFactory = new CustomerChildFactory(customers, worklogDataManager);
        setChildren(Children.create(childFactory, false));
        setDisplayName(name);
    }

    @Override
    public Action[] getActions(boolean context) {
        Action[] result = new Action[]{
            new RefreshAction(),
            new CreateAction()
        };
        return result;
    }

    public void afterCreate(Customer wd) {
        childFactory.addCustomer(wd);
    }

    public void afterUpdate(Customer wd) {
        childFactory.updateCustomer(wd);
    }

    public void beforeDelete(Customer wd) {
        childFactory.removeCustomer(wd);
    }

    private final class RefreshAction extends AbstractAction {

        public RefreshAction() {
            putValue(Action.NAME, NbBundle.getMessage(CustomersTopComponent.class, "Refresh"));
        }

        public void actionPerformed(ActionEvent e) {
            //childFactory.refreshNodes();
        }
    }

    private final class CreateAction extends AbstractAction {

        public CreateAction() {
            putValue(Action.NAME, NbBundle.getMessage(CustomersTopComponent.class, "New"));
        }

        public void actionPerformed(ActionEvent e) {
            Customer customer = WorklogDataManager.getInstance().getNewCustomer();
            WorklogWindowManager.getInstance().openWindow(customer);
        }
    }
}
